<?php

namespace Modules\System\Services\Mds;

class MDSCommon
{

    /**
     * 数组补serverUrl
     * @return string
     */
    static public function arrayDddServerUrl($list,$fieldArr){
        if(empty($list)){
            return [];
        }
        $list = $list->toArray();
        foreach($list as $k=>$v){
            foreach($v as $k2=>$v2){{
                if(in_array($k2,$fieldArr)){
                    $list[$k][$k2] = self::serverUrl($v2);
                }
            }}
        }
        return $list;

    }

    /**
     * 获取服务器网址,serverUrl
     * @return string
     */
    static function serverUrl($url){
        if (preg_match('/(http:\/\/)|(https:\/\/)/i', $url)) {
            return $url;
        }
        return "http://".$_SERVER['HTTP_HOST'].$url;
    }

    /**
     * get_ip() 获取真实IP函数
     * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
     * @return string
     */
    static function get_true_ip($type = 0){

        $type = $type ? 1 : 0;
        static $ip = null;
        if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),"unknown"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"),"unknown"))
            $ip = getenv("REMOTE_ADDR");
        else if(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],"unknown"))
            $ip = $_SERVER['REMOTE_ADDR'];
        else
            $ip = "unknown";
        $long = sprintf("%u",ip2long($ip));
        $ip = $long ? array($ip,$long) : array('0.0.0.0',0);
        return $ip[$type];

    }

    /**
     * 计算两个经纬度之间的距离
     * $miles::英里
     */
    static function distance($lat1,$lng1,$lat2,$lng2,$miles = false,$digit = 2){

        $pi80 = M_PI / 180;
        $lat1 *= $pi80;
        $lng1 *= $pi80;
        $lat2 *= $pi80;
        $lng2 *= $pi80;
        $r = 6372.797; // mean radius of Earth in km
        $dlat = $lat2 - $lat1;
        $dlng = $lng2 - $lng1;
        $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
        $c = 2 * atan2(sqrt($a),sqrt(1 - $a));
        $km = $r * $c;
        $distance = ($miles ? ($km * 0.621371192) : $km);
        $distance = round($distance,$digit);
        return $distance;

    }

    /**
     * 得到不同的时间
     */
    static function getTime($type){
        switch($type){
            case "today":
                $data['begin'] = date('Y-m-d').' 00:00:00';
                $data['end'] = date('Y-m-d').' 23:59:59';
                break;
            case "yesterday":
                $data['begin'] = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
                $data['end'] = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y'))-1);
                break;
            case "week":
                $timestamp = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y'));
                $data['begin'] = date('Y-m-d H:i:s', $timestamp);

                $timestamp = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'));
                $data['end'] = date('Y-m-d H:i:s', $timestamp);
                break;
            case "month":
                $timestamp = mktime(0, 0, 0, date('m'), 1, date('Y'));
                $data['begin'] = date('Y-m-d H:i:s', $timestamp);
                $timestamp = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
                $data['end'] = date('Y-m-d H:i:s', $timestamp);
                break;
            case "lastMonth":
                $data['begin'] = date('Y-m-01 00:00:00',strtotime('-1 month'));
                $data['end'] = date("Y-m-d 23:59:59", strtotime(-date('d').'day'));
                break;
            case "year":
                $timestamp = mktime(0, 0, 0, 1, 1, date('Y'));
                $data['begin'] = date('Y-m-d H:i:s', $timestamp);
                $data['end'] = date('Y-m-d H:i:s');
                break;
        }
        return $data;
    }


}